package com.leantech.android.browser.ui.setting

import android.os.Bundle
import androidx.annotation.StringRes
import com.leantech.android.browser.R
import com.leantech.android.browser.databinding.ActivitySettingBinding
import com.leantech.android.browser.ui.BaseActivity
import com.leantech.android.browser.ui.SimpleBrowserActivity
import com.leantech.android.browser.utils.UrlProvider
import com.leantech.android.browser.utils.onThrottleClick
import com.qmuiteam.qmui.widget.grouplist.QMUICommonListItemView
import com.qmuiteam.qmui.widget.grouplist.QMUIGroupListView.Section
import org.koin.android.ext.android.inject
import splitties.activities.start
import splitties.resources.str
import splitties.resources.txt

class SettingActivity : BaseActivity() {

    private lateinit var binding: ActivitySettingBinding
    private val urlProvider by inject<UrlProvider>()


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivitySettingBinding.inflate(layoutInflater)
        setContentView(binding.root)

        initView()
    }

    private fun initView() {
        binding.topBar.addLeftBackImageButton().onThrottleClick {
            finish()
        }
        binding.topBar.setTitle(R.string.title_setting)

        Section(this).setShowSeparator(false)
            .addItemView(createItem(R.string.text_browser_setting)) {
                start<BrowserSettingActivity>()
            }.addItemView(createItem(R.string.text_setting_user_agreement)) {
                //用户协议
                openUrl(
                    urlProvider.getServiceUrl(), str(R.string.text_setting_user_agreement)
                )
            }.addItemView(createItem(R.string.text_setting_privacy)) {
                //隐私政策
                openUrl(
                    urlProvider.getPrivacyUrl(), str(R.string.text_setting_privacy)
                )
            }.addItemView(createItem(R.string.text_setting_about)) {
                //关于
                start<AboutUsActivity>()

            }.addItemView(createItem(R.string.text_setting_feedback)) {
                //用户反馈
                start<FeedbackActivity>()

            }.addTo(binding.menuList)
    }

    private fun createItem(@StringRes title: Int, icon: Int = 0): QMUICommonListItemView {
        return binding.menuList.createItemView(txt(title)).apply {
            accessoryType = QMUICommonListItemView.ACCESSORY_TYPE_CHEVRON
            if (icon != 0) {
                setImageDrawable(getDrawable(icon))
            }
        }
    }

    private fun openUrl(url: String, title: String) {
        SimpleBrowserActivity.launch(this, url, title)
    }
}